//
//  PSBadgePool.m
//  PSControls
//
//  Created by Matias Ginart on 11/04/11.
//  Copyright 2011 Casa. All rights reserved.
//

#import "PSBadgePool.h"
#import "PSBadge.h"

@implementation PSBadgePool

PSBadgePool* sharedBadgePool;

#pragma mark - Initializers

+ (id)sharedBadgePool {
    @synchronized(self) {
        if (!sharedBadgePool) {
            sharedBadgePool = [[PSBadgePool alloc] init];
        }
        return sharedBadgePool;
    }
}

- (id)init {
    self = [super init];
    if (self) {
        badges = [[NSMutableArray alloc] init];
    }
    return self;
}

- (PSBadge*)badgeView {
    if (badges.count != 0) {
        PSBadge* badgeView = [[badges objectAtIndex:0] retain];
        [badges removeObjectAtIndex:0];
        return [badgeView autorelease];
    } else {
        PSBadge* badgeView = [[PSBadge alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
        return [badgeView autorelease];
    }
}

- (void)badgeViewIsReadyToBeReused:(PSBadge*)badgeView {
    [badges addObject:badgeView];
}

- (void)dealloc {
    [badges release];
    [super dealloc];
}

@end
